package com.wandian.texas;

import com.wandian.texas.protocol.UserProtocol;
import com.wandian.util.Log;

/**
 * @author long
 * @QQ 108232706
 * @version 1.0
 */
public class SessionMgrImp extends SessionMgr {

	private static SessionMgrImp instance = null;

	public SessionMgrImp(Single single) throws Throwable {
		super(new TexasIoHandler());
		if (single == null) {
			throw new IllegalArgumentException("Can\'t create instance , Single is Null!");
		}
	}

	// public Map<SessionObject, Short> commandMap
	public synchronized static SessionMgrImp getInstance() {
		if (instance == null) {
			try {
				syncInit();
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
		return instance;
	}

	private static synchronized void syncInit() throws Throwable {
		if (instance == null) {
			instance = new SessionMgrImp(new Single());
		}
	}

	/* 有玩家连接socket */
	@Override
	public void OnWaitCheck(SessionObject sessionObj, BaseCommandObject command) {
		try {
			// 将玩家状态设置成已接受连接，若需要可以返回个欢迎
			sessionObj.setStatus(GameData.WAITINGCHECKSUCC);
			if (command != null) {
				// 命令如果不push到线程池，必须手动回收
				ThreadPool.getInstance().freeUsedCommand(command);
			}
		} catch (Exception e) {
			Log.info("OnWaitCheck Push command failure!", e);
		}
	}

	/* 检查登陆 */
	@Override
	public void OnCheck(SessionObject sessionObj, BaseCommandObject command) {
		// 玩家状态为1,在等待外面调用检查，
		try {
			if (command == null) {
				return;
			}
			if (command.getCmd() != UserProtocol.CMD_LOGIN)// 第一个包一定是关于1000登陆
			{
				// 命令如果不push到线程池，必须手动回收
				ThreadPool.getInstance().freeUsedCommand(command);
				return;
			}
			sessionObj.cmdStatus[1] = 1;
			// byte[] bytes = dataPacket == null ? null : dataPacket.bytes;
			ThreadPool.getInstance().Push(command);
			// ThreadPool.getInstance().Push(sessionObj, dataPacket.cmdId,
			// bytes);
		} catch (Exception e) {
			Log.info("OnCheck Push command failure!", e);
		}
	}

	@Override
	public void OnConnect(SessionObject sessionObj, BaseCommandObject command) {
		// 停止外面，该玩家已经验证通过，等待置为Ready状态
		try {
			// byte[] bytes = dataPacket == null ? null : dataPacket.bytes;
			// ThreadPool.getInstance().Push(sessionObj, (short) 3, bytes);
			// 停止外面，该玩家已经验证通过，等待置为Ready状态
			sessionObj.setStatus(GameData.READYGAME);
			if (command == null) {
				return;
			} else {
				// 命令如果不push到线程池，必须手动回收
				ThreadPool.getInstance().freeUsedCommand(command);
			}

		} catch (Exception e) {
			Log.info("OnConnect Push command failure!", e);
		}
	}

	@Override
	public void OnDisconncet(SessionObject sessionObj, BaseCommandObject command) {
		// 告诉外界，这个人离开啦
		try {
			if (command != null) {
				// 命令如果不push到线程池，必须手动回收
				ThreadPool.getInstance().freeUsedCommand(command);
			}
		} catch (Exception e) {
			Log.info("OnConnect Push command failure!", e);
		}
	}

	@Override
	public void OnCommand(SessionObject sessionObj, BaseCommandObject command) {
		try {
			if (command == null) {
				return;
			}
			ThreadPool.getInstance().Push(command);
		} catch (Exception e) {
			Log.info("OnCommand Push command failure!", e);
		}
	}

	@Override
	public void OnCheckSuss(SessionObject sessionObj, BaseCommandObject command) {
		Log.info("OnCheckSuss");
		if (command != null) {
			// 命令如果不push到线程池，必须手动回收
			ThreadPool.getInstance().freeUsedCommand(command);
		}
	}

}
